package com.yangcsoft.modules.deptmanage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yangcsoft.common.utils.ExcelUtil;
import com.yangcsoft.common.utils.PageUtils;
import com.yangcsoft.common.utils.R;
import com.yangcsoft.common.validator.ValidatorUtils;
import com.yangcsoft.modules.deptmanage.entity.ContractPayItemEntity;
import com.yangcsoft.modules.deptmanage.service.ContractPayItemService;
import com.yangcsoft.modules.sys.service.SysDictService;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 合同节点付款信息表
 *
 * @author XueQing Wang
 * @email 17621152225@163.com
 * @date 2019-05-02 14:22:44
 */
@RestController
@RequestMapping("deptmanage/contractpayitem")
public class ContractPayItemController {
    @Autowired
    private ContractPayItemService contractPayItemService;
    @Autowired
    private SysDictService sysDictService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("deptmanage:contractpayitem:list")
    public R list(@RequestParam Map<String, Object> params) {
        String key = "";
        Object keyObj = params.get("key");
        if (keyObj != null) {
            key = keyObj.toString();
        }
        params.put("key", key);
        PageUtils page = contractPayItemService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{payItemNo}")
    @RequiresPermissions("deptmanage:contractpayitem:info")
    public R info(@PathVariable("payItemNo") Integer payItemNo) {
        ContractPayItemEntity contractPayItem = contractPayItemService.selectById(payItemNo);

        return R.ok().put("contractPayItem", contractPayItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("deptmanage:contractpayitem:save")
    public R save(@RequestBody ContractPayItemEntity contractPayItem) {
        contractPayItemService.save(contractPayItem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("deptmanage:contractpayitem:update")
    public R update(@RequestBody ContractPayItemEntity contractPayItem) {
        ValidatorUtils.validateEntity(contractPayItem);
        contractPayItemService.updateAllColumnById(contractPayItem);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("deptmanage:contractpayitem:delete")
    public R delete(@RequestBody Integer[] payItemNos) {
        contractPayItemService.deleteBatchIds(Arrays.asList(payItemNos));

        return R.ok();
    }

    /**
     * 导出报表
     *
     * @param request
     * @return
     */
    @RequestMapping("/export")
    public R export(Long[] ids, HttpServletRequest request,HttpServletResponse response) {
        String[] secondTitles = null;
        String[] beanProperty = null;
        String title = null;
        secondTitles = new String[]{
                "付款节点序号",
                "付款节点金额",
                "付款节点状态(已付，未付)",
                "合同序号"
        };
        beanProperty = new String[]{
                "payItemNo",
                "payItemMoney",
                "payItemStatus",
                "contractNo"
        };
        title = "合同节点付款信息表报表";

        List<ContractPayItemEntity> list = contractPayItemService.selectList(new EntityWrapper<>());
        Map<String,String> payItemStatus = sysDictService.selectExportDictMap("payItemStatus");
        for (ContractPayItemEntity entity : list) {
            entity.setPayItemStatus(payItemStatus.get(entity.getPayItemStatus()));
        }
        File file = ExcelUtil.exportResult(request, list, title, secondTitles, beanProperty);


        try {
            response.setContentType("application/x-download");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(title.getBytes("gbk"), "iso8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            FileInputStream fileInputStream = new FileInputStream(file);
            IOUtils.copy(fileInputStream, outputStream);
            outputStream.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.ok();
    }

    /**
     * 导入报表
     *
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/batchImport")
    public R batchImport(HttpServletResponse response, MultipartFile file) throws Exception {
        String[] beanProperty = null;
        beanProperty = new String[]{
                "payItemMoney",
                "payItemStatus",
                "contractNo"
        };
        List<ContractPayItemEntity> list = ExcelUtil.parserExcel(ContractPayItemEntity.class, file, beanProperty);
        Map<String,String> payItemStatus = sysDictService.selectDictMap("payItemStatus");
        for (ContractPayItemEntity entity : list) {
            entity.setPayItemStatus(payItemStatus.get(entity.getPayItemStatus()));
            save(entity);
        }
        return R.ok();
    }

    /**
     * 下载导入模版
     */
    @RequestMapping("/templateDownload")
    public R exportXls(HttpServletResponse response) {
        String title = "合同节点付款信息表录入模版";
        String[] secondTitles = new String[]{
                "付款节点金额",
                "付款节点状态(已付，未付)",
                "合同序号"
        };

        Integer startRow = 0;
        Integer endRow = 0;
        Integer startCol = 0;
        Integer endCol = 0;
        HSSFWorkbook workbook = ExcelUtil.createWorkBook(response,title,title,secondTitles);
        workbook = ExcelUtil.setValidationCell(workbook, sysDictService.selectDictListByCode("payItemStatus"),1,100,1,1);
        ExcelUtil.templateDownload(response,workbook,secondTitles,startRow,endRow,startCol,endCol);
//        ExcelUtil.templateDownload(response, title, title, secondTitles, new ArrayList<>(), startRow, endRow, startCol, endCol);

        return R.ok();
    }

}
